package com.xinongbest.course.service.impl;

import com.xinongbest.course.client.CourseListClient;
import com.xinongbest.course.dao.CoursePriceDao;
import com.xinongbest.course.entity.Course;
import com.xinongbest.course.entity.CourseListAndPrice;
import com.xinongbest.course.service.CoursePriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class CoursePriceServiceImpl implements CoursePriceService {
    @Autowired
    CoursePriceDao coursePriceDao;

    @Autowired
    CourseListClient courseListClient;

    @Override
    public Integer getCoursePriceById(Integer courseId) {
        return coursePriceDao.getPriceById(courseId);
    }

    @Override
    public List<CourseListAndPrice> getCourseListAndPrice() {
        List<CourseListAndPrice> result = new ArrayList<>();
        List<Course> courseList = courseListClient.courseList();
        CourseListAndPrice courseListAndPrice;
        if (!courseList.isEmpty()) {
            for (Course course : courseList) {
                courseListAndPrice = new CourseListAndPrice();
                courseListAndPrice.setId(course.getId());
                courseListAndPrice.setCourseId(course.getCourseId());
                courseListAndPrice.setName(course.getCourseName());
                courseListAndPrice.setPrice(getCoursePriceById(course.getCourseId()));
                result.add(courseListAndPrice);
            }
        }
        return result;
    }
}
